package com.designoutput.ui.gallery
{
	import com.designoutput.ui.GalleryCanvas;
	import com.designoutput.ui.newControls.ControlBase;
	import com.designoutput.vo.GalleryVo;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * The Navigation for gallery. It will contain navigation items.
	 */	
	public class GalleryNavigation extends Sprite
	{
		private var _canvas:GalleryCanvas;
		private var _navItemsArr:Array;
		private var _navItemsHolder:Sprite;
		private var _currentNavItem:GalleryNavigationItem;
		private var _currentGalleryIndex:Number = 0;
		
		public function GalleryNavigation(canvas:GalleryCanvas)
		{
			_canvas = canvas;
			_navItemsArr = [];	
			_navItemsHolder = new Sprite;
			_navItemsHolder.x = 0;
			_navItemsHolder.y = 10;
			addChild(_navItemsHolder);
		}
		
		/**
		 * Set-up the navigation list.
		 * @param arr
		 */		
		public function buildNavList(arr:Array):void{
			clearNavList();
			
			for(var i:int=0;i<arr.length;i++){
				var navItem:GalleryNavigationItem = new GalleryNavigationItem();
				var galleryVo:GalleryVo = GalleryVo(arr[i]);
				navItem.label = galleryVo.name;
				navItem.index = i;
				navItem.x = 10;
				navItem.y = navItem.height*i;
				navItem.addEventListener(MouseEvent.MOUSE_DOWN,galleryNavigationItemDown);
				_navItemsArr.push(navItem);
				if(i==0) {
					navItem.Mark(true);
					_currentNavItem = navItem;
				}
				_navItemsHolder.addChild(navItem);
			}
			
			reDrawBorder();
		}
		
		/**
		 * Remove exist navigation items before update the result
		 */		
		private function clearNavList():void{
			for(var i:int = 0; i < _navItemsArr.length; i++){
				_navItemsArr[i].removeEventListener(MouseEvent.MOUSE_DOWN, galleryNavigationItemDown);
			}
			
			while(_navItemsHolder.numChildren){
				_navItemsHolder.removeChildAt(0);
			}
			
			_navItemsArr = [];
		}
		
		public function reDrawBorder():void{
			graphics.clear();
			if(_navItemsArr.length>0){
				graphics.beginFill(0xD3D6D3,1);
				graphics.drawRoundRect(0,0,220, _navItemsArr.length*32+20,15,15);
				graphics.endFill();
			}
		}
		
		/**
		 * Show images for the given galley
		 * @param evt
		 */		
		private function galleryNavigationItemDown(evt:MouseEvent):void{
			if(_currentNavItem != null){
				_currentNavItem.Mark(false);
			}
			_currentNavItem = evt.currentTarget as GalleryNavigationItem;
			_currentNavItem.Mark(true);
			var index:Number = _currentNavItem.index;
			if(index == _currentGalleryIndex){
				return;
			}
			_canvas.loadGallery(index);
			_currentGalleryIndex = index;
		}
	}
}